<?php
/**
 * Medical Study Search Add Partner Popup
 *
 * This popup allows the user to add existing partners
 * to the current Medical Study or Medical Center
 *
 */


use Nawa\MedicalStudy;
use Nawa\MedicalStudy\MedicalCenter;
use Nawa\Customer;
use Nawa\MedicalStudy\Role;
use Fox\Html\CheckBox;
use Fox\Dao\SqlFragment;
use Fox\Html\Input;
use Fox\Html\TextArea;
use Doctrine\DBAL\Connection;
use Fox\Generator;
use Fox\Html\DropList;
use Fox\Html\Screen;


$xview = Xview::instance();
$id = $xview->val('customerid');
$msid = Xview::instance()->val('msid');
$mcid = Xview::instance()->val('mcid');
$keys = $xview->val(array('msid','mcid','monitoringid'));
$monitoringid = Xview::instance()->val('monitoringid');
$excluded = array();

$searchHelp = new Fox\Html\SearchHelp('MedicalStudyPartners');
$searchHelp->width(500);
$searchHelp->setMultiple(new Fox\Html\Button('select',Generator::reloadFromSelectedLines(
							'#b_GRMedicalStudyPartners',
							'MC_gPartners',
							Generator::url('medicalstudies',
											'partners',
											null,
											$keys),
							"hide(\'sh:MedicalStudyPartners\')",
							"InputSerialize(\'BC_bPartners\')"
							)));
								
$searchHelp->switchToObjectMode();
$ilink = $xview->currentURL();

$searchHelp->getSelBlock()
	->add(new TextArea('excluded'))
	->excluded()
	->hidden();
$searchHelp->getSelBlock()
	->add(new Fox\Html\Input('customerid',false,$id))
	->excluded()
	->hidden();
	
// Load already selected Product
$bPartner = Xview::instance()->component('medicalstudies', 'partners',array('xvtrs' => false));
$data = $bPartner->item('gPartners')->get(true);
if(is_array($data['gPartners']))
{
	foreach($data['gPartners'] as $line)
	{
		if(isset($line['customerid']))
		$Ids[] = $line['customerid'];
	}

	if(!empty($id))
	$Ids[] = $id;
	if(isset($Ids))
	{
		$excluded = new \Fox\Dao\SqlFragment('t.customerid NOT IN (?) ',array($Ids),array(Connection::PARAM_STR_ARRAY));
		$searchHelp->getSelBlock()->item('excluded')->default = addslashes(json_encode($Ids));
	}
}
else
{
	$values = Xview::instance()->val('excluded');
	if(!empty($values))
	{
		$Ids = json_decode(stripcslashes($values),true);
		$excluded = new \Fox\Dao\SqlFragment('t.customerid NOT IN (?) ',array($Ids),array(Connection::PARAM_STR_ARRAY));
	}
}
// Filter By Customer Type
$finder = new Role();
$roles = $finder->find(new SqlFragment('language = ?',array(Xview::instance()->language())));
$searchHelp->getSelBlock()
	->item('function')
	->options($roles,Generator::useAttribute('role'),Generator::useAttribute('label'));
	
// Add to display only user that are inside the study and not in the medical center
$searchHelp->getSelBlock()
	->item('filter_on')
	->initialize(true,true)
	->excluded()
	->renderingMode(CheckBox::RMODE_WRAP_LABEL)
	->label(Msg(Nawa\Messages::BUNDLE,Nawa\Messages::FILTER_ON_USER_STUDY));
	
if(empty($keys['mcid'])) {
	$searchHelp->getSelBlock()->item('filter_on')->hide();	
} elseif(!empty($keys['monitoringid'])) {
	$searchHelp->getSelBlock()->item('filter_on')
				->initialize(1)
				->hide();
	$force = true;
}

if(isset($_POST['dbcnt']) || $force)
{
	$inputs = $searchHelp->screen()->get();
	$searchHelp->block()->collapsed(true);
	$joins[] = Customer::$JOIN_ADDRESS;
	if(!empty($keys['monitoringid'])) {
		$joins[] = MedicalCenter::$JOIN_CUSTOMERS;
		$fragments[] = new SqlFragment('pa_mc.msid = ? AND pa_mc.mcid = ?',array($keys['msid'],$keys['mcid']));
		
	} elseif(!empty($mcid) && $inputs['filter_on']) {
		$joins[] = MedicalStudy::$JOIN_CUSTOMERS;
		$fragments[] = new SqlFragment('pa.msid = ?',array($keys['msid']));
	}
	$fragments[] = $searchHelp->screen()->fragments();
	if(isset($excluded)) {
		$fragments[] = $excluded;
	}
	$fragments[] = new SqlFragment('type = ?',array(Customer::TYPE_EMPLOYEE));
	// Handle Attaching a partner in the medical center view 
	// In this case if the filter is on we will only display partner that are attached
	// to the medical study
	$finder = new Customer();
	$res = $finder->find(Fox\Dao\SqlFragment::combine($fragments),array('name1' => false),
						$joins,$inputs['dbcnt']);
	$searchHelp
		->grid()
		->fieldcat()->setGenerators(array('function' => Generator::useFunction(function ($data){
			$role = $data->role();
			if(!empty($role))
				return Role::labelize($data->role());}),'email1' => Generator::call('email1'),
		));
	$searchHelp->grid()->getFieldcat()->field('customerid')->hidden(true);
	$searchHelp->grid()->initialize($res);
	if(!isset($_POST['dbcnt'])) {
		echo $searchHelp->display();
	} else  {
		echo $searchHelp->grid()->Display();
	}
	
}
else
{
	echo $searchHelp->display();
}